package com.fortune.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.DStoreTransExcution;
import com.fortune.dto.in.dStoreTrans.DStoreTransAListDto;
import com.fortune.dto.in.dStoreTrans.DStoreTransAddDto;
import com.fortune.dto.in.dStoreTrans.DStoreTransEditDto;
import com.fortune.entity.DUserInfo;
import com.fortune.enums.DStoreTransEnum;
import com.fortune.service.DStoreTransService;
import com.fortune.vo.dStoreTrans.DStoreTransInfo;
import com.fortune.vo.dStoreTrans.DstoreTransDetail;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(value = "dStoreTrans", description = "经销商仓库调拨", tags = {"经销商仓库调拨"})
@RestController
@RequestMapping("dStoreTrans")
public class DStoreTransController {
	
	@Autowired
	private DStoreTransService ds;
	@ApiOperation(value = "调拨单列表", nickname = "dStoreTransList", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParams({
		@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
		@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
		@ApiImplicitParam(name="search", value="搜索关键词",paramType="query",dataType="string")
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<DStoreTransInfo>> list(HttpServletRequest request,@Validated @ApiIgnore DStoreTransAListDto listDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		listDto.setPage((listDto.getPage()-1)*10);
		PagingData<DStoreTransInfo> pagingData = ds.pagingData(dUserInfo, listDto);
		return new DStoreTransExcution<PagingData<DStoreTransInfo>>(pagingData, DStoreTransEnum.SUCCESS);
	}
	@ApiOperation(value = "调拨单详情", nickname = "dStoreTransDetail", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParam(name="id", value="调拨单id",paramType="query",dataType="string",required=true)
    @GetMapping(value = "detail",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<DstoreTransDetail> detail(HttpServletRequest request,@Validated @NotBlank(message="调拨单id不能为空")String id) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		DstoreTransDetail detail = ds.dstoreTransDetail(dUserInfo, id);
		if(detail==null)
			return new DStoreTransExcution<DstoreTransDetail>(null, DStoreTransEnum.FALSE);
		return new DStoreTransExcution<DstoreTransDetail>(detail, DStoreTransEnum.SUCCESS);	
	}
	@ApiOperation(value = "添加调拨单", nickname = "mStoreTransAdd", notes = "", tags={ "@岳希伟" })
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody DStoreTransAddDto addDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		int add = ds.add(dUserInfo, addDto);
		if(add==1)
			return new DStoreTransExcution<Void>(null, DStoreTransEnum.SUCCESS);
		return new DStoreTransExcution<Void>(null, DStoreTransEnum.FALSE);
	}
	
	@ApiOperation(value = "编辑调拨单", nickname = "mStoreTransEdit", notes = "", tags={ "@岳希伟" })
    @PutMapping(value = "edit",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody DStoreTransEditDto editDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		int edite = ds.edit(dUserInfo, editDto);
		if(edite==1)
			return new DStoreTransExcution<Void>(null, DStoreTransEnum.SUCCESS);
		return new DStoreTransExcution<Void>(null, DStoreTransEnum.FALSE);
	}
	
	@ApiOperation(value = "删除调拨单", nickname = "mStoreTransDelete", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParam(name="id", value="调拨单id",paramType="query",dataType="string",required=true)
    @DeleteMapping(value = "delete",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="调拨单id不能为空")String id) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		int delete = ds.delete(dUserInfo, id);
		if(delete==1)
			return new DStoreTransExcution<Void>(null, DStoreTransEnum.SUCCESS);
		return new DStoreTransExcution<Void>(null, DStoreTransEnum.FALSE);
	}
	
	@ApiOperation(value = "出库", nickname = "mStoreTransOutConfirm", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParam(name="id", value="调拨单id",paramType="query",dataType="string",required=true)
    @PutMapping(value = "outConfirm",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> outConfirm(HttpServletRequest request,@Validated @NotBlank(message="调拨单id不能为空")String id) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		int result = ds.outConfirm(dUserInfo, id);
		if(result==1)
			return new DStoreTransExcution<Void>(null, DStoreTransEnum.SUCCESS);
		return new DStoreTransExcution<Void>(null, DStoreTransEnum.FALSE);
	}
	
	@ApiOperation(value = "入库", nickname = "mStoreTransInConfirm", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParam(name="id", value="调拨单id",paramType="query",dataType="string",required=true)
    @PutMapping(value = "inConfirm",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> inConfirm(HttpServletRequest request,@Validated @NotBlank(message="调拨单id不能为空")String id) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		int result = ds.inConfirm(dUserInfo, id);
		if(result==1)
			return new DStoreTransExcution<Void>(null, DStoreTransEnum.SUCCESS);
		return new DStoreTransExcution<Void>(null, DStoreTransEnum.FALSE);
	}
}
